﻿using System;
using System.Collections.Specialized;
using System.Windows.Forms;
using Core.ApiMetods.Abstract;
using Core.DependencyInjection;
using Core.Enum;
using Core.Manager;
using DomainModels.DomainModels.JsonModels;
using DounloderV2WinFormsUI.Forms;
using DounloderV2WinFormsUI.ResoucesStrings;

namespace DounloderV2WinFormsUI
{
    public partial class MainForm : Form
    {
        private AppStatus AppStatus { get; set; }

        private readonly IManager _manager;

        public MainForm(IManager manager)
        {
            _manager = manager;
            InitializeComponent();

            AppStatus = AppStatus.NotLogin;
            tabControl.Enabled = AppStatus != AppStatus.NotLogin;
        }

        /// <summary>
        /// Init Settings.
        /// </summary>
        /// <param name="sender">Object sender.</param>
        /// <param name="e">Parameters.</param>
        private void MainForm_Load(object sender, EventArgs e)
        {
            OnStatusChande(AppStatus.NotLogin);
        }

        /// <summary>
        /// Exit from program.
        /// </summary>
        /// <param name="sender">Object sender.</param>
        /// <param name="e">Parameters.</param>
        private void AppExitMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        /// <summary>
        /// FormClosing event handler.
        /// </summary>
        /// <param name="sender">Object sender.</param>
        /// <param name="e">Parameters.</param>
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (AppStatus == AppStatus.Login || AppStatus == AppStatus.Work)
            {
                var result = MessageBox.Show(GlobalStrings.MessageExit, GlobalStrings.CaptionExit,
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question);
                e.Cancel = (result == DialogResult.No);
                // todo stop all theads
            }
        }

        /// <summary>
        /// LoginMenuItem Click event handler.
        /// </summary>
        /// <param name="sender">Object sender.</param>
        /// <param name="e">Parameters.</param>
        private void LoginMenuItem_Click(object sender, EventArgs e)
        {
            switch (AppStatus)
            {
                case AppStatus.Login:
                    Text = GlobalStrings.AppName;
                    OnStatusChande(AppStatus.NotLogin);
                    break;
                case AppStatus.Work:
                    // todo stop all works.
                    break;
                case AppStatus.NotLogin:
                    LoginToVk();
                    break;
                default:
                    throw new ArgumentException("Error status");
            }
        }

        private void ShowDialogs(Form form)
        {
            Enabled = false;
            form.ShowDialog();
            Enabled = true;
        }

        private void OnStatusChande(AppStatus status)
        {
            AppStatus = status;
            switch (AppStatus)
            {
                case AppStatus.NotLogin:
                    loginToolStripMenuItem.Text = GlobalStrings.Login;
                    break;
                case AppStatus.Login:
                    loginToolStripMenuItem.Text = GlobalStrings.Logout;
                    scopesToolStripMenuItem.Enabled = false;
                    tabControl.Enabled = true;
                    break;
            }
        }

        private void LoginToVk()
        {
            var webLoginForms = NinjectFactory.Resolve<WebLoginForms>();
            webLoginForms.OnLogin += WebLoginFormsOnLogin;
            ShowDialogs(webLoginForms);
        }

        private void WebLoginFormsOnLogin(object sender, EventArgs eventArgs)
        {
            var tokenData = sender as NameValueCollection;
            if (tokenData != null)
            {
                _manager.SetToken(tokenData);
                var user = _manager.GetMethod(new UserFactory()).Get();
                SetLoginInfo(user as Users);
            }
        }

        private void SetLoginInfo(Users user)
        {
            if (user != null)
            {
                Text = string.Format("{0} {1}", Text, string.Format("{0} {1} {2}", user.first_name, user.last_name, user.mobile_phone));
                AppStatus = AppStatus.Login;
                OnStatusChande(AppStatus.Login);
                mainUserInfoPage.SetUser(user);
            }
        }
    }
}
